package com.action.pc.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bean.Js;
import com.bean.Xtcd;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.github.pagehelper.Page;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.service.user.JsService;
import com.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by lvjianqing on 2017/9/10.
 */
@Controller
@RequestMapping("role")
public class JsAction extends BaseAction {
    Logger log = Logger.getLogger(JsAction.class);
    @Autowired
    private JsService jsService;

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, ModelMap modelMap, JsVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        HashMap<String, Object> paraMap = new HashMap<>();
        Page page = jsService.findByPage(pageNum, pageSize, paraMap);
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/js_list", modelMap);
    }

    @RequestMapping("modify")
    public ModelAndView modify(HttpServletRequest request, ModelMap modelMap, JsVo vo) {
        Long id = vo.getJsid();
        Js js = new Js();
        if (id != null) {
            js = jsService.findById(id);
            modelMap.put("js", js);
        }
        return new ModelAndView("/pc/js_add", modelMap);
    }

    @RequestMapping("save")
    public ModelAndView save(HttpServletRequest request, ModelMap modelMap, JsVo vo) {
        Js js = new Js();
        js.setId(vo.getJsid());
        js.setMc(vo.getMc());
        js.setOrders(vo.getOrders());
        jsService.saveJs(js);
        return new ModelAndView("redirect:/role/list.htm", modelMap);
    }

    @RequestMapping("del")
    @ResponseBody
    public String jsDel(HttpServletRequest request, ModelAndJson modelMap, JsVo vo) {
        Long id = vo.getJsid();
        if (id != null) {
            jsService.deleteById(id);
        }
        modelMap.setTrue("删除成功");
        return modelMap.toString();
    }

    @RequestMapping("qxtree")
    public ModelAndView qxTree(HttpServletRequest request, ModelMap modelMap, JsVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        Long id = vo.getJsid();
        modelMap.put("jsid", id);
        Page page = jsService.findXtcdByPage(pageNum, 0);
        Multimap<String, JSONObject> multimap = ArrayListMultimap.create();
        HashMap<String, Xtcd> pmap = new HashMap<>();
        List<Long> ids = jsService.findByJsId(id);
        if (page != null) {
            List<Xtcd> list = page.getResult();
            for (Xtcd cd : list) {
                if (! "0".equals(cd.getSjbm())) {
                    JSONObject jo = new JSONObject();
                    jo.put("title", cd.getMkmc());
                    jo.put("type", "item");
                    JSONObject j = new JSONObject();
                    j.put("id", cd.getId());
                    jo.put("attr", j);
                    multimap.put(cd.getSjbm(), jo);
                } else {
                    pmap.put(cd.getBm(), cd);
                }
            }
        }
        JSONArray ja = new JSONArray();
        Set<String> set = pmap.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String key = it.next();
            Xtcd cd = pmap.get(key);
            JSONObject jo = new JSONObject();
            jo.put("title", cd.getMkmc());
            jo.put("type", "folder");
            JSONObject j = new JSONObject();
            j.put("id", cd.getId());
            jo.put("attr", j);
            jo.put("children", multimap.get(key));
            ja.add(jo);
        }
        modelMap.put("tree", ja.toString());
        modelMap.put("ids", ids);
        return new ModelAndView("/pc/qx_tree", modelMap);
    }

    @RequestMapping("qxtreesave")
    @ResponseBody
    public String qxTreeSave(HttpServletRequest request, ModelAndJson modelMap, JsVo vo, String ids) {
        Long jsid = vo.getJsid();
        if (StringUtil.isNotEmpty(ids)) {
            String[] idss = ids.split(",");
            Long[] idsl = new Long[idss.length];
            for (int i = 0; i < idss.length; i++) {
                idsl[i] = StringUtil.String2Long(idss[i], -1L);
            }
            jsService.saveJsqx(jsid, idsl);
        }
        return modelMap.toString();
    }

    @RequestMapping("yhjsfp")
    public ModelAndView yhjs(HttpServletRequest request, ModelMap modelMap, JsVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        Integer yhid = vo.getId();
        modelMap.put("yhid", yhid);
        Page page = jsService.findByPage(pageNum, 0, new HashMap<String, Object>());
        Multimap<Long, JSONObject> multimap = ArrayListMultimap.create();
        HashMap<Long, String> pmap = new HashMap<>();
        List<Long> ids = jsService.findJsIdByYhId(yhid);
        if (page != null) {
            List<Js> list = page.getResult();
            for (Js js : list) {
                if (js.getSjid() != null && js.getSjid() > 0) {
                    JSONObject jo = new JSONObject();
                    jo.put("title", js.getMc());
                    jo.put("type", "item");

                    JSONObject j = new JSONObject();
                    j.put("id", String.valueOf(js.getId()));
                    jo.put("attr", j);
                    multimap.put(js.getSjid(), jo);
                } else {
                    pmap.put(js.getId(), js.getMc());
                }
            }
        }
        JSONArray ja = new JSONArray();
        Set<Long> set = pmap.keySet();
        Iterator<Long> it = set.iterator();
        while (it.hasNext()) {
            Long key = it.next();
            JSONObject jo = new JSONObject();
            jo.put("title", pmap.get(key));
            jo.put("type", "folder");
            jo.put("children", multimap.get(key));
            ja.add(jo);
        }
        modelMap.put("tree", ja.toString());
        modelMap.put("ids", ids);
        return new ModelAndView("/pc/js_tree", modelMap);
    }

    @RequestMapping("jstreesave")
    @ResponseBody
    public String jsTreeSave(HttpServletRequest request, ModelAndJson modelMap, JsVo vo, String ids) {
        Integer yhid = vo.getId();
        if (StringUtil.isNotEmpty(ids)) {
            String[] idss = ids.split(",");
            Long[] idsl = new Long[idss.length];
            for (int i = 0; i < idss.length; i++) {
                idsl[i] = StringUtil.String2Long(idss[i], -1L);
            }
            jsService.saveYhjs(yhid, idsl);
        }
        return modelMap.toString();
    }
}
